﻿using EnterpriseWebsiteApi.ActionFilters;
using EnterpriseWebsiteApi.ActionResults;
using EnterpriseWebsiteApi.Dto.ProductPriceUnit;
using EnterpriseWebsiteBLL;
using EnterpriseWebsiteEntity;
using EnterpriseWebsiteUtil.Helper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EnterpriseWebsiteApi.Controllers.Admin
{
    [Route("api/admin/[controller]/[action]")]
    [ApiController]
    [TypeFilter(typeof(JwtAuthorizeFilter))]
    public class ProductPriceCurrenciesController : ControllerBase
    {
        private readonly ProductPriceCurrenciesService _productPriceCurrenciesService;
        private readonly Mapper _mapper;

        public ProductPriceCurrenciesController(ProductPriceCurrenciesService productPriceCurrenciesService, Mapper mapper)
        {
            _productPriceCurrenciesService = productPriceCurrenciesService;
            _mapper = mapper;
        }

        /// <summary>
        /// 插入或更新
        /// </summary>
        /// <param name="products"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> InsertOrUpdate(IEnumerable<ProductPriceCurrenciesDTO> products)
        {
            try
            {
                var entitys = _mapper.MapList<ProductPriceCurrenciesEntity>(products);
                await _productPriceCurrenciesService.InsertOrUpdate(entitys);
                return new Success();
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }

        /// <summary>
        /// 获取全部商品货币
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetAllProductPriceCurrencies()
        {
            try
            {
                var entitys = await _productPriceCurrenciesService.GetAllProductPriceCurrencies();
                var dto = _mapper.MapList<ProductPriceCurrenciesDTO>(entitys);
                return new Success(dto);
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }

        /// <summary>
        /// 根据ID删除一个商品货币
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> DeleteProductPriceCurrenciesById(int id)
        {
            try
            {
                await _productPriceCurrenciesService.DeleteProductPriceCurrenciesById(id);
                return new Success();
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }
    }
}
